/* http client library for restdb/httpd server */
package http

import (
	"io"
	"os"
	"json"
	"bytes"
	"http"
	"log"
)

const Path = "http://root.cloud9lib.net/db"

func PublishReader (collection string, doc io.Reader) os.Error {

	_, err := Do("POST", "c=" + collection, doc)

	return err
}

func Publish (collection string, obj interface{}) os.Error {

	text, err := json.Marshal(obj)
	
	log.Println(string(text))

	if err != nil {
			return err
	}

	return PublishReader(collection, bytes.NewBuffer(text))
}

func Do(method, params string, reader io.Reader) (*http.Response, os.Error) {

	req, err := http.NewRequest(method, Path + "?" + params, reader)

	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-type", "text/json")

	resp, err := http.DefaultClient.Do(req)

	if err != nil {
		return nil, err
	}

	return resp, err
}
